<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class AddRole extends HTML_QuickForm {
  private $tpl;
  private $role;

  function AddRole($tpl,$role) {
    $this->tpl = $tpl;
    $this->role = $role;

    parent::HTML_QuickForm('addrole');
    $this->addElement('header', 'header', 'addrole');
    $this->addElement('text', 'role', _('role'), array('size' => 20, 'maxlength' => 32));
    $this->addElement('submit', 'btn_Submit', _('submit'), array('class' => 'submit', 'onmouseover' => 'this.style.backgroundColor = \'#E0E7E7\'; this.style.borderColor = \'#728FA4\';', 'onmouseout' => 'this.style.backgroundColor = \'#eeeeee\'; this.style.borderColor = \'#999999\';'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('check_exists', 'function', 'checkExists', $this);
    $this->addRule('role', _('role required'), 'required');
    $this->addRule('role', _('min lenght 3'), 'minlength', '3');
    $this->addRule('role', _('characters not allowed'), 'nopunctuation');
    $this->addRule('role', _('role exists'), 'check_exists');
  }

  function checkExists() {
    $this->role->setName($this->getSubmitValue('role'));
    return $this->role->checkExists($op = 'add');
  }

  function displayForm() {
    $this->renderer =& new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->role->getMsg());
  }

  function process() {
    $this->role->addRole();
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->role->getMsg());
  }

} // end of class

?>